package MyFitnessPlate.Handler;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import MyFitnessPlate.DataSet.ParsedLoginInfoDataSet;

public class LoginInfoHandler extends DefaultHandler {
	/*
	 * <user> <id>{id}</id> <username>{username}</username>
	 * <firstname>{firstname}</firstname> <lastname>{lastname}</lastname>
	 * <email>{email}</email> <gender>{gender 1 if male, 0 if female}</gender>
	 * <weight>{weight in pounds}</weight> <height>{height in inches}</height>
	 * <age>{age}</age> </user>
	 */
	// ===========================================================
	// Fields
	// ===========================================================
	@SuppressWarnings("unused")
	private boolean in_xml = false;
	@SuppressWarnings("unused")
	private boolean in_check_user = false;
	@SuppressWarnings("unused")
	private boolean in_user = false;
	private boolean in_success = false;
	private boolean in_id = false;
	private boolean in_username = false;
	private boolean in_firstname = false;
	private boolean in_lastname = false;
	private boolean in_email = false;
	private boolean in_gender = false;
	private boolean in_weight = false;
	private boolean in_height = false;
	private boolean in_age = false;

	private ParsedLoginInfoDataSet myParsedLoginInfoDataSet = new ParsedLoginInfoDataSet();

	// ===========================================================
	// Getter & Setter
	// ===========================================================

	public ParsedLoginInfoDataSet getParsedData() {
		return this.myParsedLoginInfoDataSet;
	}

	// ===========================================================
	// Methods
	// ===========================================================
	@Override
	public void startDocument() throws SAXException {
		this.myParsedLoginInfoDataSet = new ParsedLoginInfoDataSet();
	}

	@Override
	public void endDocument() throws SAXException {
		// Nothing to do
	}

	/**
	 * Gets be called on opening tags like: <tag> Can provide attribute(s), when
	 * xml was like: <tag attribute="attributeValue">
	 */
	@Override
	public void startElement(String namespaceURI, String localName,
			String qName, Attributes atts) throws SAXException {
		if (localName.equals("xml")) {
			this.in_xml = true;
		} else if (localName.equals("check-user")) {
			this.in_check_user = true;
		} else if (localName.equals("success")) {
			this.in_success = true;
		} else if (localName.equals("user")) {
			this.in_user = true;
		} else if (localName.equals("id")) {
			this.in_id = true;
		} else if (localName.equals("username")) {
			this.in_username = true;
		} else if (localName.equals("firstname")) {
			this.in_firstname = true;
		} else if (localName.equals("lastname")) {
			this.in_lastname = true;
		} else if (localName.equals("email")) {
			this.in_email = true;
		} else if (localName.equals("gender")) {
			this.in_gender = true;
		} else if (localName.equals("weight")) {
			this.in_weight = true;
		} else if (localName.equals("height")) {
			this.in_height = true;
		} else if (localName.equals("age")) {
			this.in_age = true;
		}
	}

	/**
	 * Gets be called on closing tags like: </tag>
	 */
	@Override
	public void endElement(String namespaceURI, String localName, String qName)
			throws SAXException {
		if (localName.equals("xml")) {
			this.in_xml = false;
		} else if (localName.equals("check-user")) {
			this.in_check_user = false;
		} else if (localName.equals("success")) {
			this.in_success = false;
		} else if (localName.equals("user")) {
			this.in_user = false;
		} else if (localName.equals("id")) {
			this.in_id = false;
		} else if (localName.equals("username")) {
			this.in_username = false;
		} else if (localName.equals("firstname")) {
			this.in_firstname = false;
		} else if (localName.equals("lastname")) {
			this.in_lastname = false;
		} else if (localName.equals("email")) {
			this.in_email = false;
		} else if (localName.equals("gender")) {
			this.in_gender = false;
		} else if (localName.equals("weight")) {
			this.in_weight = false;
		} else if (localName.equals("height")) {
			this.in_height = false;
		} else if (localName.equals("age")) {
			this.in_age = false;
		}
	}

	/**
	 * Gets be called on the following structure: <tag>characters</tag>
	 */
	@Override
	public void characters(char ch[], int start, int length) {
		if (this.in_success) {
			myParsedLoginInfoDataSet.setsuccess(new String(ch, start, length).trim());
		}
		if (this.in_id) {
			myParsedLoginInfoDataSet.setid(new String(ch, start, length));
		}
		if (this.in_username) {
			myParsedLoginInfoDataSet.setusername(new String(ch, start, length));
		}
		if (this.in_firstname) {
			myParsedLoginInfoDataSet.setfirstname(new String(ch, start, length));
		}
		if (this.in_lastname) {
			myParsedLoginInfoDataSet.setlastname(new String(ch, start, length));
		}
		if (this.in_email) {
			myParsedLoginInfoDataSet.setemail(new String(ch, start, length));
		}
		if (this.in_gender) {
			myParsedLoginInfoDataSet.setgender(new String(ch, start, length));
		}
		if (this.in_weight) {
			myParsedLoginInfoDataSet.setweight(new String(ch, start, length));
		}
		if (this.in_height) {
			myParsedLoginInfoDataSet.setheight(new String(ch, start, length));
		}
		if (this.in_age) {
			myParsedLoginInfoDataSet.setage(new String(ch, start, length));
		}
	}
}